package m.jclecai.com.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.ProofBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.MyToast;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;


/**
 * 忘记密码
 */
public class ResetPassWordActivity extends BaseActivity {


    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_graph_code)
    EditText etGraphCode;
    //图形验证码
    @BindView(R.id.iv_gf)
    ImageView gf_graph_code;
    private String userName,title;

    @Override
    public int getContentView() {
        return R.layout.activity_reset_pass_word;
    }

    @Override
    public void initData() {
        userName = getIntent().getStringExtra("userName");
        title = getIntent().getStringExtra("title");
        if(TextUtils.isEmpty(title))
            title = "忘记密码";
        setTitle(title);
        leftOptionText("");
        getImgCode();
    }

    @Override
    public void initView() {
    }

    @OnClick(R.id.text_bottom)
    void next() {
        //如果是从用户名跳转
        if(TextUtils.isEmpty(userName)){
            checkPhone();
        }else {
            checkNameAndPhone();
        }
    }

    @OnClick(R.id.iv_gf)
    void getImgCode() {
        Glide.with(this).load(Constant_Url.REGISTER_RAND_URL)
                .diskCacheStrategy(DiskCacheStrategy.NONE)//禁用磁盘缓存
                .skipMemoryCache(true).into(gf_graph_code);//跳过内存缓存
    }



    /**
     * 校验手机号和用户名是否匹配
     */
    private void checkNameAndPhone() {
        showDialog();
        HashMap<String, String> pair = new HashMap<>();
        pair.put("user",userName);
        pair.put("phone",etPhone.getText().toString());
        HttpManager.postAsyn(Constant_Url.CHECK_NAME_AND_PHONE, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                DisPlay(getString(R.string.error_network));
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                ProofBean proofBean = FastOk.get(response, ProofBean.class);
                if(proofBean != null && "0".equals(proofBean.getStatus())){
                    getUserNameByPhoneAndName();
                }else {
                    MyToast.showToast(ResetPassWordActivity.this,"用户名、手机号码不匹配");
                }
            }
        },pair);
    }

    private void getUserNameByPhoneAndName(){
        showDialog();
        HashMap<String, String> pair = new HashMap<>();
        pair.put("user",userName);
        pair.put("phone",etPhone.getText().toString());
        pair.put("sessionid","");//图形验证码
        pair.put("flag","2");
        HttpManager.postAsyn(Constant_Url.FIND_PWD_BY_MOBILE, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                DisPlay(getString(R.string.error_network));
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                ProofBean proofBean = FastOk.get(response, ProofBean.class);
                MyToast.showToast(ResetPassWordActivity.this,proofBean.getMessage());
                if(proofBean != null && "0".equals(proofBean.getStatus())){
                    //存在此用户名，跳转
                    Intent intent = new Intent(ResetPassWordActivity.this,PasswordValidateActivity.class);
                    intent.putExtra("userName",userName);
                    intent.putExtra("title",title);
                    intent.putExtra("phone",etPhone.getText().toString());
                    startActivity(intent);
                    finish();
                }
            }
        },pair);
    }


    /**
     * 校验手机号
     */
    private void checkPhone() {
        showDialog();
        HashMap<String, String> pair = new HashMap<>();
        pair.put("phone",etPhone.getText().toString());
        HttpManager.postAsyn(Constant_Url.CHECK_USER_MOBILE, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                DisPlay(getString(R.string.error_network));
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                ProofBean proofBean = FastOk.get(response, ProofBean.class);
                if(proofBean != null && "1".equals(proofBean.getStatus())){
                    getUserNameByPhone();
                }else {
                    MyToast.showToast(ResetPassWordActivity.this,"手机号不存在");
                }
            }
        },pair);
    }

    private void getUserNameByPhone(){
        showDialog();
        HashMap<String, String> pair = new HashMap<>();
        pair.put("phone",etPhone.getText().toString());
        pair.put("sessionid","");//图形验证码
        HttpManager.postAsyn(Constant_Url.FIND_NAME_BY_MOBILE, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                DisPlay(getString(R.string.error_network));
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                ProofBean proofBean = FastOk.get(response, ProofBean.class);
                MyToast.showToast(ResetPassWordActivity.this,proofBean.getMessage());
                if(proofBean != null && "0".equals(proofBean.getStatus())){
                    //存在此用户名，跳转
                    Intent intent = new Intent(ResetPassWordActivity.this,PasswordValidateActivity.class);
                    intent.putExtra("phone",etPhone.getText().toString());
                    intent.putExtra("title",title);
                    startActivity(intent);
                    finish();
                }
            }
        },pair);
    }
}
